#include "reason.h"
#include "ui_reason.h"
#include <QString>
#include <QStringList>
#include <QDebug>
#include <QMessageBox>
QString REASON;//
QString REASONID;//

Reason::Reason(datebase *db,QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Reason)
{
    ui->setupUi(this);
    ui->pushButton->setFocusPolicy(Qt::NoFocus);
    Tool_Visible();
    this->myDb = db;
    databse_check(&this->myDb->sqlite_db);
    /*QObjectList list = children();
      QToolButton *b;
      foreach (QObject *obj, list)
      {
          b = qobject_cast<QToolButton*>(obj);
          if(b){
              qDebug() <<b->objectName();
               }
      }*/
}

Reason::~Reason()
{
    delete ui;
}

bool Reason::Tool_SetTex(int count,QString str)
{
    if(count==1 && ui->toolButton->text().isEmpty())
    {
        ui->toolButton->setText(str);
      //ui->toolButton->setEnabled(false);//控件变灰色
        ui->toolButton->setVisible(true);//控件显示
        count=2;
    }else if(count==2 &&ui->toolButton_2->text().isEmpty())
    {
        ui->toolButton_2->setText(str);
        ui->toolButton_2->setVisible(true);//控件显示
        count=3;
    }else if(count==3 &&ui->toolButton_3->text().isEmpty())
    {
        ui->toolButton_3->setText(str);
        ui->toolButton_3->setVisible(true);//控件显示
        count=4;
    }else if(count==4 &&ui->toolButton_4->text().isEmpty())
    {
        ui->toolButton_4->setText(str);
        ui->toolButton_4->setVisible(true);//控件显示
        count=5;
    }else if(count==5 &&ui->toolButton_5->text().isEmpty())
    {
        ui->toolButton_5->setText(str);
        ui->toolButton_5->setVisible(true);//控件显示
        count=6;
    }else if(count==6 &&ui->toolButton_6->text().isEmpty())
    {
        ui->toolButton_6->setText(str);
        ui->toolButton_6->setVisible(true);//控件显示
        count=7;
    }else if(count==7 &&ui->toolButton_7->text().isEmpty())
    {
        ui->toolButton_7->setText(str);
        ui->toolButton_7->setVisible(true);//控件显示
        count=8;
    }else if(count==8 &&ui->toolButton_8->text().isEmpty())
    {
        ui->toolButton_8->setText(str);
        ui->toolButton_8->setVisible(true);//控件显示
        count=9;
    }else if(count==9 &&ui->toolButton_9->text().isEmpty())
    {
        ui->toolButton_9->setText(str);
        ui->toolButton_9->setVisible(true);//控件显示
        count=10;
    }else if(count==10 &&ui->toolButton_10->text().isEmpty())
    {
        ui->toolButton_10->setText(str);
        ui->toolButton_10->setVisible(true);//控件显示
        count=11;
    }else if(count==11 &&ui->toolButton_11->text().isEmpty())
    {
        ui->toolButton_11->setText(str);
        ui->toolButton_11->setVisible(true);//控件显示
        count=12;
    }else if(count==12 &&ui->toolButton_12->text().isEmpty())
    {
        ui->toolButton_12->setText(str);
        ui->toolButton_12->setVisible(true);//控件显示
        count=13;
    }
}

void Reason::databse_check(QSqlDatabase *db)
{
    QSqlQuery *query;
    QString str,id,reason;
    query = new QSqlQuery(*db);
    query->clear();
    query->prepare(QObject::tr("select id,reason from hand"));
    query->exec();
    int met=1;
    while(query->next())
    {
     id=query->value(0).toString();
     reason=query->value(1).toString();
     str=id.append(".").append(reason);
     Tool_SetTex(met,str);
     met++;
    }
    if(met==1)
    {
      Tool_SetTex(met,QString("1.人工起杆"));
    }
}

void Reason::on_toolButton_clicked()
{
    if(ui->toolButton->text().isEmpty())
    {
       QMessageBox::information(this,QObject::trUtf8("提示"), QObject::trUtf8("请选择其他原因"));
       return;
    }
    QStringList list;
    list=ui->toolButton->text().split(".");
    REASONID=list[0];
    REASON=list[1];
    this->close();
}

void Reason::on_toolButton_2_clicked()
{
    if(ui->toolButton_2->text().isEmpty())
    {
       QMessageBox::information(this,QObject::trUtf8("提示"), QObject::trUtf8("请选择其他原因"));
       return ;
    }
    QStringList list;
    list=ui->toolButton_2->text().split(".");
    REASONID=list[0];
    REASON=list[1];
    this->close();
}


void Reason::on_toolButton_3_clicked()
{
    if(ui->toolButton_3->text().isEmpty())
    {
       QMessageBox::information(this,QObject::trUtf8("提示"), QObject::trUtf8("请选择其他原因"));
       return ;
    }
    QStringList list;
    list=ui->toolButton_3->text().split(".");
    REASONID=list[0];
    REASON=list[1];
    this->close();
}

void Reason::on_toolButton_4_clicked()
{
    if(ui->toolButton_4->text().isEmpty())
    {
       QMessageBox::information(this,QObject::trUtf8("提示"), QObject::trUtf8("请选择其他原因"));
       return ;
    }
    QStringList list;
    list=ui->toolButton_4->text().split(".");
    REASONID=list[0];
    REASON=list[1];
    this->close();
}

void Reason::on_toolButton_5_clicked()
{
    if(ui->toolButton_5->text().isEmpty())
    {
       QMessageBox::information(this,QObject::trUtf8("提示"), QObject::trUtf8("请选择其他原因"));
       return ;
    }
    QStringList list;
    list=ui->toolButton_5->text().split(".");
    REASONID=list[0];
    REASON=list[1];
    this->close();
}

void Reason::on_toolButton_6_clicked()
{
    if(ui->toolButton_6->text().isEmpty())
    {
       QMessageBox::information(this,QObject::trUtf8("提示"), QObject::trUtf8("请选择其他原因"));
       return ;
    }
    QStringList list;
    list=ui->toolButton_6->text().split(".");
    REASONID=list[0];
    REASON=list[1];
    this->close();
}

void Reason::on_toolButton_7_clicked()
{
    if(ui->toolButton_7->text().isEmpty())
    {
       QMessageBox::information(this,QObject::trUtf8("提示"), QObject::trUtf8("请选择其他原因"));
       return ;
    }
    QStringList list;
    list=ui->toolButton_7->text().split(".");
    REASONID=list[0];
    REASON=list[1];
    this->close();
}

void Reason::on_toolButton_8_clicked()
{
    if(ui->toolButton_8->text().isEmpty())
    {
       QMessageBox::information(this,QObject::trUtf8("提示"), QObject::trUtf8("请选择其他原因"));
       return ;
    }
    QStringList list;
    list=ui->toolButton_8->text().split(".");
    REASONID=list[0];
    REASON=list[1];
    this->close();
}

void Reason::on_toolButton_9_clicked()
{
    if(ui->toolButton_9->text().isEmpty())
    {
       QMessageBox::information(this,QObject::trUtf8("提示"), QObject::trUtf8("请选择其他原因"));
       return ;
    }
    QStringList list;
    list=ui->toolButton_9->text().split(".");
    REASONID=list[0];
    REASON=list[1];
    this->close();
}

void Reason::on_toolButton_10_clicked()
{
    if(ui->toolButton_10->text().isEmpty())
    {
       QMessageBox::information(this,QObject::trUtf8("提示"), QObject::trUtf8("请选择其他原因"));
       return ;
    }
    QStringList list;
    list=ui->toolButton_10->text().split(".");
    REASONID=list[0];
    REASON=list[1];
    this->close();
}

void Reason::on_toolButton_11_clicked()
{
    if(ui->toolButton_11->text().isEmpty())
    {
       QMessageBox::information(this,QObject::trUtf8("提示"), QObject::trUtf8("请选择其他原因"));
       return ;
    }
    QStringList list;
    list=ui->toolButton_11->text().split(".");
    REASONID=list[0];
    REASON=list[1];
    this->close();
}

void Reason::on_toolButton_12_clicked()
{
    if(ui->toolButton_12->text().isEmpty())
    {
       QMessageBox::information(this,QObject::trUtf8("提示"), QObject::trUtf8("请选择其他原因"));
       return ;
    }
    QStringList list;
    list=ui->toolButton_12->text().split(".");
    REASONID=list[0];
    REASON=list[1];
    this->close();
}

void Reason::closeEvent(QCloseEvent *event)//触发关闭函数
{
    event->accept();  //接受退出信号，程序退出
}

void Reason::on_pushButton_clicked()
{

    REASON="N";
    REASONID="N";
    this->close();
}

void Reason::paintEvent(QPaintEvent *)//背景
{
   QPixmap pix("images/picR.jpg");
   QPainter painter(this);
   int w = width();
   int h = height();
   painter.drawPixmap(0,0,w,h,pix);
}

void Reason::Tool_Visible()
{
    ui->toolButton->setVisible(false);//控件隐藏
    ui->toolButton_2->setVisible(false);//控件隐藏
    ui->toolButton_3->setVisible(false);//控件隐藏
    ui->toolButton_4->setVisible(false);//控件隐藏
    ui->toolButton_5->setVisible(false);//控件隐藏
    ui->toolButton_6->setVisible(false);//控件隐藏
    ui->toolButton_7->setVisible(false);//控件隐藏
    ui->toolButton_8->setVisible(false);//控件隐藏
    ui->toolButton_9->setVisible(false);//控件隐藏
    ui->toolButton_10->setVisible(false);//控件隐藏
    ui->toolButton_11->setVisible(false);//控件隐藏
    ui->toolButton_12->setVisible(false);//控件隐藏
}
